// pages/groupBuy/shop/index/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        title: '',//标题
        category: [],//类目
        image: '',//图片
        price: 0,//团购价
        original_price: 0,//商品原价
        hair_number: 0,//发放数量
        introduce: [],//图文介绍
        goods_details: [],//商品明细
        validity_days: 0,//购买后有效天数
        use_time: '',//使用时间
        is_reserve: 0,//是否预约
        remarks: [],//购买须知明细
        isShowClass: false,//是否打开商品类目弹窗
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.id && options.id>0){
            this.setData({
                id: options.id
            })
            this.getInfo();
        }
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //打开弹窗
    onOpen: function(e) {
        let name = e.currentTarget.dataset.name;
        this.setData({
            [name]: true
        })
    },
    //接收商品类目数据
    confirmClass: function(e) {
        let obj = e.detail;
        this.setData({
            isShowClass: false
        })
        if(obj && obj.change){
            this.setData({
                category: obj.selected
            })
        }
    },
    //上传图片
    uploadImg: function (e) {
        let _this = this;
        let {introduce} = this.data;
        let name = e.currentTarget.dataset.name;
        let count = 1;
        if(name == 'introduce'){
            count = 10-introduce.length;
        }
        wx.$utils.uploadImg({count: count,},function(url) {
            if(name == 'image'){
                _this.setData({
                    image: url
                })
            }else if(name == 'introduce'){
                introduce.push(url);
                _this.setData({
                    introduce: introduce
                })
            }
        })
    },
    //预览图片
    reviewImg: function (e) {
        let name = e.currentTarget.dataset.name;
        let index = e.currentTarget.dataset.index;
        let current = '';
        let urls = [];
        if(name == 'image'){
            current = this.data.image;
            urls = [this.data.image];
        }else if(name == 'introduce'){
            let {introduce} = this.data;
            current = introduce[index];
            urls = introduce;
        }
        wx.previewImage({
            current: current,
            urls: urls
        })
    },
    //删除图片
    removeImg: function (e) {
        let _this = this;
        let {introduce} = this.data;
        let name = e.currentTarget.dataset.name;
        let index = e.currentTarget.dataset.index;
        wx.showModal({
            title: '提示',
            content: '确定要删除吗',
            success: function (res) {
                if (res.confirm) {
                    if(name == 'image'){
                        _this.setData({
                            image: ''
                        })
                    }else if(name == 'introduce'){
                        introduce.splice(index,1);
                        _this.setData({
                            introduce: introduce
                        })
                    }
                }
            }
        })
    },
    //设置数字
    setNumber: function(e) {
        let name = e.currentTarget.dataset.name;
        let v = Number(e.detail.value) || 0;
        this.setData({
            [name]: v
        })
    },
    //添加明细
    addObj: function(e) {
        let name = e.currentTarget.dataset.name;
        let obj = {key: '',value: ''};
        this.data[name].push(obj);
        this.setData({
            [name]: this.data[name]
        })
    },
    //设置明细key
    setKey: function(e) {
        let index = e.currentTarget.dataset.index;
        let name =  e.currentTarget.dataset.name;
        let v = e.detail.value;
        this.data[name][index].key = v;
        this.setData({
            [name]: this.data[name]
        })
    },
    //设置明细value
    setValue: function(e) {
        let index = e.currentTarget.dataset.index;
        let name =  e.currentTarget.dataset.name;
        let v = e.detail.value;
        this.data[name][index].value = v;
        this.setData({
            [name]: this.data[name]
        })
    },
    //移除明细
    removeItem: function(e) {
        let _this = this;
        let index = e.currentTarget.dataset.index;
        let name =  e.currentTarget.dataset.name;
        wx.showModal({
            title: '提示',
            content: '确定要移除吗？',
            complete: (res) => {
                if (res.confirm) {
                    _this.data[name].splice(index,1);
                    _this.setData({
                        [name]: _this.data[name]
                    })
                }
            }
        })
    },

    //设置预约选项
    setReserve: function() {
        let _this = this;
        wx.showActionSheet({
            itemList: ['需要预约','不需要预约'],
            success (res) {
                let tapIndex = res.tapIndex;
                _this.setData({
                    is_reserve: tapIndex+1
                })
            },
        })
    },
    //保存并预览
    save: function() {
        let {id, title, category, image, price, original_price, hair_number, introduce, goods_details, validity_days, use_time, is_reserve, remarks } = this.data;
        let goodsFlag = true;
        let remarkFlag = true;
        if(!title || !title.trim()){
            wx.showToast({
                title: '请输入商品标题',
                icon: 'none'
            })
            return;
        }
        if(!category.length){
            wx.showToast({
                title: '请选择商品类目',
                icon: 'none'
            })
            return;
        }else{
            category.forEach(item=>{
                delete item.isCur;
            })
        }
        if(!image){
            wx.showToast({
                title: '请上传商品图片',
                icon: 'none'
            })
            return;
        }
        if(price<=0){
            wx.showToast({
                title: '请设置团购金额',
                icon: 'none'
            })
            return;
        }
        if(original_price<=0){
            wx.showToast({
                title: '请设置原价',
                icon: 'none'
            })
            return;
        }
        goods_details.forEach(item=>{
            if(!item.key || !item.key.trim() || !item.value || !item.value.trim()){
                goodsFlag = false;
            }
        })
        if(!goodsFlag){
            wx.showToast({
                title: '商品明细不能为空',
                icon: 'none'
            })
            return;
        }
        if(validity_days<=0){
            wx.showToast({
                title: '请设置有效期',
                icon: 'none'
            })
            return;
        }
        if(!use_time || !use_time.trim()){
            wx.showToast({
                title: '请设置使用时间',
                icon: 'none'
            })
            return;
        }
        remarks.forEach(item=>{
            if(!item.key || !item.key.trim() || !item.value || !item.value.trim()){
                remarkFlag = false;
            }
        })
        if(!remarkFlag){
            wx.showToast({
                title: '须知明细不能为空',
                icon: 'none'
            })
            return;
        }
        wx.$http({
            load: true,
            url: '/business/group-goods-save',
            data: {
                id: id,
                title: title,
                category: JSON.stringify(category),
                image: image,
                price: price,
                original_price: original_price,
                hair_number: hair_number,
                introduce: JSON.stringify(introduce),
                goods_details: JSON.stringify(goods_details),
                validity_days: validity_days,
                use_time: use_time,
                is_reserve: is_reserve,
                remarks: JSON.stringify(remarks)
            },
        }).then((res) => {
            let data = res.data;
            if(data.id){
                wx.redirectTo({
                    url: `/pages/groupBuy/shop/review/index?id=${data.id}`,
                })
            }
        })
    },
    //获取详情
    getInfo: function() {
        let {id} = this.data;
        wx.$http({
            load: true,
            method: 'get',
            url: `/business/group-goods-info/${id}`,
            data: {},
        }).then((res) => {
            let data = res.data;
            this.setData({
                id: data.id,
                title: data.title,
                category: data.category,
                image: data.image,
                price: data.price,
                original_price: data.original_price,
                hair_number: data.hair_number,
                introduce: data.introduce,
                goods_details: data.goods_details,
                validity_days: data.validity_days,
                use_time: data.use_time,
                is_reserve: data.is_reserve,
                remarks: data.remarks
            })
        })
    }
})